<?php 
    class Admin_Model_User extends Zend_Db_Table_Abstract{
        protected $_name = 'user';
        protected $_primary = 'user_id';
        protected $_db;
        
        public function __construct() {
            $this->_db = Zend_Registry::get('db');
        }
        
        public function getFullnameById($user_id) {
            $sql = $this->_db->select()
                            ->from($this->_name, array('fullname'))
                            ->where('user_id = ?', $user_id);
            $result = $this->_db->fetchOne($sql);
            return $result;
        }
        
        public function listUsers(){
			$sql="select user_id,fullname,date_format(birthday,'%m/%d/%Y') as birthday,email,phone,avatar from user";
			return $this->_db->fetchAll($sql);
		}
        public function listRole(){
           $sql ="select * from role";
           return $this->_db->fetchAll($sql);
        }
        public function list_User(){
            $sql = $this->_db->select()
                            ->from($this->_name);
            $result = $this->_db->fetchAll($sql);
            return $result;
        }
        public function listUser($id){
            $sql = $this->_db->select()
                            ->from('user')
                            ->where('user_id= ?',$id);
            $res = $this->_db->fetchAll($sql);
            return $res;
        }
        public function Users($email){
            $sql="select * from user where email='$email'";
			return $this->_db->fetchAll($sql);
        }
        public function addUser($data){
            $sql = $this->_db->insert($this->_name, $data);
            $result = $this->_db->lastInsertId($this->_name, $this->_primary);
            return $result;
        }
        public function editUser($data,$user_id){
            $where = $this->getAdapter()->quoteInto('user_id = ?', $user_id);
            $this->_db->update($this->_name, $data, $where);
    
        }
        public function editUserNoavatar($data,$user_id){
            $where = $this->getAdapter()->quoteInto('user_id = ?', $user_id);
            $this->_db->update($this->_name, $data, $where);
        }
         public function changpass($id,$newpass){
            $rs = $this->_db->update('user',
                array('password'  =>"$newpass",     
                ),'user_id='.$id
            );
        }
      
        public function deleteUser($id){
            $where = $this->getAdapter()->quoteInto('user_id = ?', $id);
            $this->_db->delete($this->_name, $where);
            $index = Zend_Search_Lucene::open('files/user');
            $hits = $index->find('user_id:' . $id);           
            foreach($hits as $hit){
                $index->delete($hit->id);
            }
            $index->commit();
            $index->optimize();
        }
      
         public function deleteUsers($id){
            $index = Zend_Search_Lucene::open('files/user');
            foreach ($id as $value){
                $where = $this->getAdapter()->quoteInto('user_id = ?', $value);
                $this->_db->delete($this->_name, $where);
                $hits = $index->find('user_id:' . $value);
                foreach($hits as $hit){
                    $index->delete($hit->id);
                }
			}
            $index->commit();
            $index->optimize();
        }
        public function randomCode($length){
            $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
            $size = strlen( $chars );
            for( $i = 0; $i < $length; $i++ ) {
              $str .= $chars[ rand( 0, $size - 1 ) ];
            }
            return $str;
        }
          public function searchUser($keyword){
            Zend_Search_Lucene_Search_QueryParser::setDefaultEncoding('utf-8');
            setlocale(LC_CTYPE, 'UTF-8');
            Zend_Search_Lucene_Analysis_Analyzer::setDefault(new Zend_Search_Lucene_Analysis_Analyzer_Common_Utf8_CaseInsensitive());
			$index = Zend_Search_Lucene::open('files/user');
            if(!empty($keyword)){
                $hits = $index->find($keyword);
                return $hits;
            }
		}
        public function saveAddItem($arrParam = null, $options = null){
            if($options['task'] == 'add')
            {
                try
                {
                    Zend_Search_Lucene_Search_QueryParser::setDefaultEncoding('UTF-8');
                    setlocale(LC_CTYPE, 'UTF-8');
                    Zend_Search_Lucene_Analysis_Analyzer::setDefault(new Zend_Search_Lucene_Analysis_Analyzer_Common_Utf8_CaseInsensitive());
                    $index = Zend_Search_Lucene::open('files/user');
                    Zend_Search_Lucene::setResultSetLimit(350);
                }
                catch(exception $e)
                {
                    Zend_Search_Lucene_Search_QueryParser::setDefaultEncoding('UTF-8');
                    setlocale(LC_CTYPE, 'UTF-8');
                    Zend_Search_Lucene_Analysis_Analyzer::setDefault(new Zend_Search_Lucene_Analysis_Analyzer_Common_Utf8_CaseInsensitive());
                    $index = Zend_Search_Lucene::create('files/user');
                }
                
                $doc = new Zend_Search_Lucene_Document();
                $doc->addField(Zend_Search_Lucene_Field::keyword('user_id', $arrParam['user_id']))
                    ->addField(Zend_Search_Lucene_Field::text('fullname', $arrParam['fullname'], 'UTF-8'))
                    ->addField(Zend_Search_Lucene_Field::text('email', $arrParam['email'], 'UTF-8'))
                    ->addField(Zend_Search_Lucene_Field::text('birthday', $arrParam['birthday'], 'UTF-8'))
                    ->addField(Zend_Search_Lucene_Field::text('avatar', $arrParam['avatar'], 'UTF-8'))
                    ->addField(Zend_Search_Lucene_Field::text('phone', $arrParam['phone'], 'UTF-8'))
                    ->addField(Zend_Search_Lucene_Field::text('user_name_filter', $arrParam['user_name_filter'], 'UTF-8'));
                    
                $index->addDocument($doc);
                $index->commit();
                $index->optimize();
            }
            
            if($options['task'] == 'edit')
            {
                try
                {
                    Zend_Search_Lucene_Search_QueryParser::setDefaultEncoding('UTF-8');
                    setlocale(LC_CTYPE, 'UTF-8');
                    Zend_Search_Lucene_Analysis_Analyzer::setDefault(new Zend_Search_Lucene_Analysis_Analyzer_Common_Utf8_CaseInsensitive());
                    $index = Zend_Search_Lucene::open('files/user');
                    Zend_Search_Lucene::setResultSetLimit(350);
                }
                catch(exception $e)
                {
                    Zend_Search_Lucene_Search_QueryParser::setDefaultEncoding('UTF-8');
                    setlocale(LC_CTYPE, 'UTF-8');
                    Zend_Search_Lucene_Analysis_Analyzer::setDefault(new Zend_Search_Lucene_Analysis_Analyzer_Common_Utf8_CaseInsensitive());
                    $index = Zend_Search_Lucene::create('files/user');
                }
                
                $hits = $index->find('user_id:'.$arrParam['user_id']);
                foreach($hits as $hit)
                {
                    $index->delete($hit->id);
                }
                
                $doc = new Zend_Search_Lucene_Document();
                $doc->addField(Zend_Search_Lucene_Field::keyword('user_id', $arrParam['user_id']))
                    ->addField(Zend_Search_Lucene_Field::text('fullname', $arrParam['fullname'], 'UTF-8'))
                    ->addField(Zend_Search_Lucene_Field::text('email', $arrParam['email'], 'UTF-8'))
                    ->addField(Zend_Search_Lucene_Field::text('birthday', $arrParam['birthday'], 'UTF-8'))
                    ->addField(Zend_Search_Lucene_Field::text('avatar', $arrParam['avatar'], 'UTF-8'))
                    ->addField(Zend_Search_Lucene_Field::text('phone', $arrParam['phone'], 'UTF-8'))
                    ->addField(Zend_Search_Lucene_Field::text('user_name_filter', $arrParam['user_name_filter'], 'UTF-8'));
                
                $index->addDocument($doc);
                $index->commit();
                $index->optimize();
            }
        }
    }

?>